import { Shop } from 'shop-model.js';
var shop = new Shop();
Page({
  data: {
    btnType: 'disable',
    money: 0,
    btnText: "确认支付",
    onLoading: false,
    rate: 0, //金额兑换积分比例
    total : 0,//充值金额能兑换到的积分
  },
  onLoad: function (options) {
    var config = wx.getStorageSync('config');
    var rate = config[13].value;
    this.setData({
      rate
    })
  },
  onReady: function () {

  },
  onShow: function () {

  },
  //按钮状态变化
  moneyChange: function (e) {
    var val = e.detail.value;
    val = parseFloat(val);
    this.setData({ money: val });
    let type = 'disable';
    if (val && val > 0) {
      this.setData({
        total: val * parseFloat(this.data.rate)
      })
      type = 'btn'
    }
    this.setData({
      btnType: type
    })
  },
  //申请提现
  withdraw: function (e) {
    if (this.data.money <= 0) {
      wx.showToast({
        title: '请填写正确的金额',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.onLoading) {
      return false;
    }
    this.data.onLoading = true;
    this.setData({ btnText: '正在充值...' })
    var that = this;
    this.data.payMethod = 0; //0微信支付，1余额支付（尚未对接）
    this.pay();
  },
  toLog:function(){
    wx.navigateTo({
      url: 'score-log?type=1',
    })
  },  //点击立即支付
  pay: function () {
    var that = this;
    //支付参数
    var params = {
      "money": this.data.money 
    }
    if (this.data.payMethod == 0) {
      //微信支付
      this._execPay(params);
    } else if (this.data.payMethod == 1) {
      //余额支付
      //传参给设置支付密码/充值后做跳转回来
      if (this.data.id) {
        var type = 1;
        var id = this.data.id;
      }
      if (this.data.parent_sn) {
        var type = 2;
        var id = this.data.parent_sn;
      }
      if (parseFloat(this.data.radioItems[1].money) < parseFloat(this.data.item.pay_amount)) {
        wx.showModal({
          title: '提示',
          content: '您的余额不足',
          confirmText: "去充值",
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../center/recharge?type=' + type + '&id=' + id
              });
            } else if (res.cancel) {
              console.log(1)
            }
          }
        })
        return false;
      }

      //没绑定手机号码，也没有设置支付密码
      if (this.data.item.set_mobile == 0 && this.data.item.set_pay_pwd == 0) {
        wx.showModal({
          title: '提示',
          content: '请先绑定您的手机并设置支付密码',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../member/binding?setPassword=1&type=' + type + '&id=' + id
              });
            } else if (res.cancel) {
              console.log(1)
            }
          }
        })
        return false;
      }
      //绑定了手机，但没有设置支付密码
      if (this.data.item.set_mobile == 1 && this.data.item.set_pay_pwd == 0) {
        wx.showModal({
          title: '提示',
          content: '请先设置支付密码',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../member/set-payment?type=' + type + '&id=' + id
              });
            } else if (res.cancel) {
              console.log(2)
            }
          }
        })
        return false;
      }
      //支付密码输入框
      this.setData({
        showInput: true
      });
    }
  },
  //微信支付
  _execPay: function (params) {
    var that = this;
    shop.execPay(params, (data) => {
      if (data == 1) {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000,
          success: function () {
            setTimeout(function () {
              wx.navigateTo({
                url: './index'
              });
            }, 2000)
          }
        })
      } else if (data == 0 ) {
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000,
          success: function () {
           
          }
        })
      }

    }, (data) => {
      this.data.onLoading = false;
      this.setData({ btnText: '确认支付' })
    });
  },
  //余额支付
  payConfirm: function () {
    var that = this;
    if (!this.data.pay_pwd) {
      wx.showToast({
        title: '请输入支付密码',
        icon: 'none',
        duration: 2000,
      })
      return false;
    }
    //支付参数
    if (this.data.id) {
      var params = {
        order_id: this.data.id
      }
    }
    if (this.data.parent_sn) {
      var params = {
        parent_sn: this.data.parent_sn
      }
    }
    params.pay_pwd = this.data.pay_pwd;
    order.accountPay(params, (data) => {
      if (data.status == 1) {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000,
          success: function () {
            setTimeout(function () {
              wx.navigateTo({
                url: '../order/list?index=0'
              });
            }, 2000)
          }
        })
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    });
  },
  //关闭弹窗
  hideModal: function (e) {
    this.setData({
      showInput: false
    })
  },
})